
from langchain.prompts import PromptTemplate

try:
    # 指定UTF-8编码来读取文件
    template = PromptTemplate.from_file("example_prompt_template.txt", encoding="utf-8")
    print("===Template===")
    print(template)
    print("===Prompt===")
    print(template.format(topic='黑色幽默'))
except UnicodeDecodeError as e:
    print(f"文件编码错误: {e}")
    print("尝试使用gbk编码...")
    try:
        template = PromptTemplate.from_file("example_prompt_template.txt", encoding="gbk")
        print("===Template===")
        print(template)
        print("===Prompt===")
        print(template.format(topic='黑色幽默'))
    except Exception as e2:
        print(f"仍然出错: {e2}")
        print("手动创建模板...")
        template = PromptTemplate(input_variables=["topic"], template="举一个关于{topic}的例子")
        print("===Template===")
        print(template)
        print("===Prompt===")
        print(template.format(topic='黑色幽默'))
except Exception as e:
    print(f"其他错误: {e}")
